package com.xb.service.imp;

import com.xb.entity.*;
import com.xb.mapper.*;
import com.xb.service.ITollService;
import com.xb.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * entityService业务层处理
 */
@Service
public class TollServiceImp implements ITollService {
    @Resource
    private TollMapper tollMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RegisterMapper registerMapper;

    @Resource
    private GetMedicalRecordMapper getMedicalRecordMapper;

    @Resource
    private MedicalRecordMapper recordMapper;

    @Resource
    private TollTakeDrugMapper tollTakeDrugMapper;


    @Override
    public Result getAllMedicalRecord(Register register) {
        Result result = new Result();
        try {
            List<Register> registerList = registerMapper.selectRegisterStatus(register);

            if (registerList != null) {
                result.setData(registerList);
                result.setTotal(1);
                result.setStatus(Result.RESPONSE_SUCCESS);
            } else {
                result.setMessage("没有查到对应的数据！");
                result.setStatus(Result.RESPONSE_FAIL);
            }
            return result;
        } catch (Exception e) {
            result.setMessage("异常！请联系管理员");
            return result;
        }

    }

    @Override
    public Result getMedicalRecord(GetMedicalRecord getMedicalRecord) {
        Result result = new Result();
        GetMedicalRecord recordByRegisterId = getMedicalRecordMapper.getMedicalRecordByRegisterId(getMedicalRecord);
        if (recordByRegisterId != null) {
            recordByRegisterId.setExaminationCost(getMedicalRecordMapper.getExaminationCost(getMedicalRecord));
            recordByRegisterId.setNowDate(new Date());
            result.setData(recordByRegisterId);
            result.setStatus(Result.RESPONSE_SUCCESS);
        }
        return result;
    }

    @Transactional
    @Override
    public Result saveTollInfo(MedicalRecord medicalRecord, HttpSession session) {
        Result result = new Result();
        try {
            String email = (String) session.getAttribute("email");
            User user = userMapper.selectUserByEmail(email);
            Register register = new Register();
            register.setId(medicalRecord.getRegisterId());
            register.setChargeStatus(1);
            if (registerMapper.updateRegister(register) == 0) {
                throw new RuntimeException("register修改失败！");
            }
            medicalRecord = recordMapper.selectMedicalRecordByPrescriptionNum(medicalRecord.getPrescriptionNum());
            TollTakeDrug tollTakeDrug = new TollTakeDrug();
            tollTakeDrug.setTakingDrugStatus(0);
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            tollTakeDrug.setTollDateTime(currentTime.format(formatter));
            tollTakeDrug.setTollOperator(String.valueOf(user.getId()));
            tollTakeDrug.setMoney(medicalRecord.getMoney());
            tollTakeDrug.setPrescriptionNum(medicalRecord.getPrescriptionNum());
            if (tollTakeDrugMapper.updateTollTakeDrug(tollTakeDrug) == 0) {
                throw new RuntimeException("register修改失败！");
            }
            result.setStatus(Result.RESPONSE_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage("提交失败！请联系管理员");
            return result;
        }

    }
}
